package top.zywork.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.common.StringUtils;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.GoodsExpressDTO;
import top.zywork.query.GoodsExpressQuery;
import top.zywork.security.SecurityUtils;
import top.zywork.service.GoodsExpressService;
import top.zywork.vo.ExpressComputeVO;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.PagerVO;
import top.zywork.vo.GoodsExpressVO;

import java.util.List;

/**
 * GoodsExpressController控制器类<br/>
 *
 * 创建于2019-11-22<br/>
 *
 * @author http://zywork.top 危锦辉
 * @version 1.0
 */
@RestController
@RequestMapping("/goods-express")
@Slf4j
public class GoodsExpressController extends BaseController {

    private GoodsExpressService goodsExpressService;

    @PostMapping("admin/save")
    @SysLog(description = "后台保存GoodsExpressVO")
    public ResponseStatusVO save(@RequestBody @Validated GoodsExpressVO goodsExpressVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        goodsExpressService.save(BeanUtils.copy(goodsExpressVO, GoodsExpressDTO.class));
        return ResponseStatusVO.ok("添加成功", null);
    }

    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存GoodsExpressVO")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<GoodsExpressVO> goodsExpressVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        goodsExpressService.saveBatch(BeanUtils.copyListObj(goodsExpressVOList, GoodsExpressDTO.class));
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/remove/{id}")
    @SysLog(description = "后台删除GoodsExpressVO")
    public ResponseStatusVO removeById(@PathVariable("id") Long id) {
        goodsExpressService.removeById(id);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @PostMapping("admin/batch-remove")
    @SysLog(description = "后台批量删除GoodsExpressVO")
    public ResponseStatusVO removeByIds(@RequestBody String[] ids) {
        goodsExpressService.removeByIds(StringUtils.strArrayToLongArray(ids));
        return ResponseStatusVO.ok("批量删除成功", null);
    }

    @PostMapping("admin/update")
    @SysLog(description = "后台更新GoodsExpressVO")
    public ResponseStatusVO update(@RequestBody @Validated GoodsExpressVO goodsExpressVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        int updateRows = goodsExpressService.update(BeanUtils.copy(goodsExpressVO, GoodsExpressDTO.class));
        if (updateRows == 1) {
            return ResponseStatusVO.ok("更新成功", null);
        } else {
            return ResponseStatusVO.dataError("数据版本号有问题，在此更新前数据已被更新", null);
        }
    }

    @PostMapping("admin/batch-update")
    @SysLog(description = "后台批量更新GoodsExpressVO")
    public ResponseStatusVO updateBatch(@RequestBody @Validated List<GoodsExpressVO> goodsExpressVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        goodsExpressService.updateBatch(BeanUtils.copyListObj(goodsExpressVOList, GoodsExpressDTO.class));
        return ResponseStatusVO.ok("批量更新成功", null);
    }

    @PostMapping("admin/active")
    @SysLog(description = "后台激活或冻结GoodsExpressVO")
    public ResponseStatusVO active(@RequestBody GoodsExpressVO goodsExpressVO) {
        goodsExpressService.update(BeanUtils.copy(goodsExpressVO, GoodsExpressDTO.class));
        return ResponseStatusVO.ok("激活或冻结成功", null);
    }

    @PostMapping("admin/batch-active")
    @SysLog(description = "后台批量激活或冻结GoodsExpressVO")
    public ResponseStatusVO activeBatch(@RequestBody @Validated List<GoodsExpressVO> goodsExpressVOList) {
        goodsExpressService.updateBatch(BeanUtils.copyListObj(goodsExpressVOList, GoodsExpressDTO.class));
        return ResponseStatusVO.ok("批量激活或冻结成功", null);
    }

    @GetMapping("admin/one/{id}")
    public ResponseStatusVO getById(@PathVariable("id") Long id) {
        GoodsExpressVO goodsExpressVO = new GoodsExpressVO();
        Object obj = goodsExpressService.getById(id);
        if (obj != null) {
            goodsExpressVO = BeanUtils.copy(obj, GoodsExpressVO.class);
        }
        return ResponseStatusVO.ok("查询成功", goodsExpressVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = goodsExpressService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GoodsExpressVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody GoodsExpressQuery goodsExpressQuery) {
        PagerDTO pagerDTO = goodsExpressService.listAllByCondition(goodsExpressQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GoodsExpressVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody GoodsExpressQuery goodsExpressQuery) {
        PagerDTO pagerDTO = goodsExpressService.listPageByCondition(goodsExpressQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GoodsExpressVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 保存商品运费模版关联数据
     * @param goodsInfoId
     * @param expressTemplateId
     * @return
     */
    @PostMapping("admin/save-data")
    @SysLog(description = "后台保存商品运费模板")
    public ResponseStatusVO saveData(Long goodsInfoId, Long expressTemplateId) {
        return goodsExpressService.saveData(goodsInfoId, expressTemplateId);
    }

    /**
     * 有用户信息获取商品运费模版
     * @param goodsId
     * @param goodsSkuId
     * @return
     */
    @PostMapping("user/get-express-template")
    public ResponseStatusVO userGetExpressTemplate(Long goodsId, Long goodsSkuId, String province) {
        return goodsExpressService.getExpressInfoByUserId(SecurityUtils.getJwtUser().getUserId(), goodsId, goodsSkuId, province);
    }

    /**
     * 没有用户信息获取商品运费模版
     * @param goodsId
     * @param goodsSkuId
     * @return
     */
    @PostMapping("any/get-express-template")
    public ResponseStatusVO anyGetExpressTemplate(Long goodsId, Long goodsSkuId) {
        return goodsExpressService.getExpressInfo(goodsId, goodsSkuId);
    }

    /**
     * 获取默认模版信息
     * @return
     */
    @GetMapping("any/get-default-express-template")
    public ResponseStatusVO anyGetDefaultExpressTemplate() {
        return goodsExpressService.getDefaultExpressInfo();
    }

    /**
     * 计算商品运费
     * @param expressComputeVOList
     * @return
     */
    @PostMapping("user/compute-express-fee")
    public ResponseStatusVO computeExpressFee(@RequestBody List<ExpressComputeVO> expressComputeVOList) {
        return goodsExpressService.computeExpressFee(SecurityUtils.getJwtUser().getUserId(), expressComputeVOList);
    }

    @Autowired
    public void setGoodsExpressService(GoodsExpressService goodsExpressService) {
        this.goodsExpressService = goodsExpressService;
    }
}
